#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QString id,QSqlDatabase* db,QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_db = *db;
    m_adminId = id;

    this->setCentralWidget(ui->stackedWidget);//设置中心组件

    iniModel();//初始化模型

    ui->stackedWidget->setCurrentWidget(ui->pagMain);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    ui->actMenuMange->trigger();
}


void MainWindow::on_pushButton_2_clicked()
{
    ui->actOrderMange->trigger();
}


void MainWindow::on_pushButton_3_clicked()
{
    ui->actSeatMange->trigger();
}


void MainWindow::on_pushButton_4_clicked()
{
    ui->actPayMange->trigger();
}


void MainWindow::on_actMenuMange_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMenu);
}

void MainWindow::iniModel()
{

    this->setWindowTitle("欢迎使用点餐管理系统!");

    ui->tvMenu->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvOrder->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvSeat->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvPay->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    m_menuModel = new QSqlTableModel(this);
    m_orderModel = new QSqlTableModel(this);
    m_seatModel = new QSqlTableModel(this);
    m_payModel = new QSqlTableModel(this);

    m_menuModel->setTable("tb_menu");
    m_orderModel->setTable("tb_order");
    m_seatModel->setTable("tb_seat");
    m_payModel->setTable("tb_pay");

    m_menuModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_orderModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_seatModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_payModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    m_menuModel->select();
    m_orderModel->select();
    m_seatModel->select();
    m_payModel->select();

    m_menuModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_menuModel->setHeaderData(1,Qt::Horizontal,"名称",Qt::DisplayRole);
    m_menuModel->setHeaderData(2,Qt::Horizontal,"价格",Qt::DisplayRole);
    m_menuModel->setHeaderData(3,Qt::Horizontal,"类型",Qt::DisplayRole);

    m_seatModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_seatModel->setHeaderData(1,Qt::Horizontal,"状态",Qt::DisplayRole);

    m_orderModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_orderModel->setHeaderData(1,Qt::Horizontal,"状态",Qt::DisplayRole);
    m_orderModel->setHeaderData(2,Qt::Horizontal,"信息",Qt::DisplayRole);
    m_orderModel->setHeaderData(3,Qt::Horizontal,"总价",Qt::DisplayRole);
    m_orderModel->setHeaderData(4,Qt::Horizontal,"座位号",Qt::DisplayRole);
    m_orderModel->setHeaderData(5,Qt::Horizontal,"时间",Qt::DisplayRole);

    m_payModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_payModel->setHeaderData(1,Qt::Horizontal,"状态",Qt::DisplayRole);
    m_payModel->setHeaderData(2,Qt::Horizontal,"名称",Qt::DisplayRole);

    ui->tvMenu->setModel(m_menuModel);
    ui->tvOrder->setModel(m_orderModel);
    ui->tvSeat->setModel(m_seatModel);
    ui->tvPay->setModel(m_payModel  );


}


void MainWindow::on_actOrderMange_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagOrder);
}


void MainWindow::on_actSeatMange_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagSeat);
}


void MainWindow::on_actPayMange_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagPay);
}


void MainWindow::on_actMain_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMain);
}



void MainWindow::on_btnAppdenMenu_clicked()
{
    m_menuModel->insertRow(m_menuModel->rowCount());
}


void MainWindow::on_btnDeleteMenu_clicked()
{
    if(ui->tvMenu->currentIndex().isValid()){
        m_menuModel->removeRow(ui->tvMenu->currentIndex().row());
    }
}


void MainWindow::on_btnResetMenu_clicked()
{
    m_menuModel->select();
}


void MainWindow::on_btnSubmitMenu_clicked()
{
    if(m_menuModel->isDirty() == true){

            m_menuModel->submitAll();

    }
}


void MainWindow::on_btnAppendSeat_clicked()
{
    m_seatModel->insertRow(m_seatModel->rowCount());
}


void MainWindow::on_btnDeleteSeat_clicked()
{
    if(ui->tvSeat->currentIndex().isValid()){
        m_seatModel->removeRow(ui->tvSeat->currentIndex().row());
    }
}


void MainWindow::on_btnResetSeat_clicked()
{
    m_seatModel->select();
}


void MainWindow::on_btnSubmitSeat_clicked()
{
    if(m_seatModel->isDirty() == true){

        m_seatModel->submitAll();

    }
}


void MainWindow::on_btnAppendPay_clicked()
{
    m_payModel->insertRow(m_payModel->rowCount());
}


void MainWindow::on_btnDeletePay_clicked()
{
    if(ui->tvPay->currentIndex().isValid()){
        m_payModel->removeRow(ui->tvPay->currentIndex().row());
    }
}


void MainWindow::on_btnResetPay_clicked()
{
    m_payModel->select();
}


void MainWindow::on_btnSubmitPay_clicked()
{
    if(m_payModel->isDirty() == true){

        m_payModel->submitAll();

    }
}


void MainWindow::on_btnDeleteOrder_clicked()
{
    if(ui->tvOrder->currentIndex().isValid()){
        m_orderModel->removeRow(ui->tvOrder->currentIndex().row());
    }
}


void MainWindow::on_btnResetOrder_clicked()
{
    m_orderModel->select();
}


void MainWindow::on_btnSubmitOrder_clicked()
{
    if(m_orderModel->isDirty() == true){

        m_orderModel->submitAll();

    }
}


void MainWindow::on_tvOrder_doubleClicked(const QModelIndex &index)
{
    QSqlQuery query;

    QString id = m_orderModel->data(m_orderModel->index(index.row(),0)).toString();
    QString state = m_orderModel->data(m_orderModel->index(index.row(),1)).toString();
    QString order = m_orderModel->data(m_orderModel->index(index.row(),2)).toString();
    QString total = m_orderModel->data(m_orderModel->index(index.row(),3)).toString();
    QString seat = m_orderModel->data(m_orderModel->index(index.row(),4)).toString();
    QString time = m_orderModel->data(m_orderModel->index(index.row(),5)).toString();

    ui->labIdOrder->setText(id);

    if(state.toInt() == 0){
        ui->labStateOrder->setText("未完成");
    }else{
        ui->labStateOrder->setText("已完成");
    }

    QString resultStr;
    QStringList tempList = order.split("&");
    for(int i = 0; i < tempList.count(); ++i){
        QStringList alist = tempList.at(i).split("*");
        for(int j = 0;j < m_menuModel->rowCount();++j){
            if(m_menuModel->data(m_menuModel->index(j,0)).toInt() == alist.at(0).toInt()){
                resultStr += m_menuModel->data(m_menuModel->index(j,1)).toString()+"*"+alist.at(1)+"  ";
                break;
            }
        }
    }
    ui->labMenuOrder->setText(resultStr);

    ui->labTotalOrdder->setText(total);

    ui->labSeatOrder->setText(seat);

    ui->labTimeOrder->setText(time);

    ui->stackedWidget->setCurrentWidget(ui->pagOrderDetail);
}

